<?php
namespace System\Core;

class Db {
    private static $instance;
    private $pdo;
    public static $sql;
    
    private function __construct() {
        $host = Config::get('DB_HOST', 'localhost');
        $dbname = Config::get('DB_NAME');
        $user = Config::get('DB_USER');
        $pass = Config::get('DB_PASS');
        
        $this->pdo = new \PDO(
            "mysql:host={$host};dbname={$dbname};charset=utf8",
            $user,
            $pass
        );
        
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
    }
    
    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    public function query($sql, $params = []) {
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($params);
        return $stmt;
    }
    
    public function lastInsertId() {
        return $this->pdo->lastInsertId();
    }
}